package com.huangyi;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import jakarta.websocket.Session;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/test")
public class TestController {
    //建立连接
    @RequestMapping(value = "/v1")
    public String p1(String name){
        return  "接收到名字:" + name;
    }
    @RequestMapping("/getuser")
    public String getUser(User user ){
        return "user: " + user;
    }
    @RequestMapping("/request")
    public String p4(HttpServletRequest request){
        String name = request.getParameter("name");
        Integer age = Integer.valueOf(request.getParameter("age"));
        return "name：" + name + "，age：" + age;
    }
    @RequestMapping("/arr")
    public String getArr(String[] arr){
        return "arr: " + List.of(arr);
    }
    @RequestMapping("/arr1")
    public String getList(@RequestParam List<String> list){
        return "list: " + list;
    }
    @RequestMapping("/arr2")
    public String getList1(List<String> list){
        return "list: " + list;
    }
    @RequestMapping("/Json")
    public String gerJson(@RequestBody User user){
        return "user: " + user;
    }
    @RequestMapping("/article/{articleID}")
    public String getId(@PathVariable String articleID){
        return "articleID: "+ articleID;
    }
    @RequestMapping("/article/{articleID}/{name}")
    public String getId2(@PathVariable String articleID,@PathVariable String name){
        return "articleID: "+ articleID + "name: " + name;
    }
    @RequestMapping("/file")
    public String getFile(MultipartFile file){
        System.out.println(file);
        return "文件夹名字: " + file.getOriginalFilename();
    }
    @RequestMapping("/getCookie")
    public String getCookie(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        for (int i = 0; i < cookies.length ; i++) {
            System.out.println(cookies.toString());
        }
        return "返回cookie成功";
    }
    @RequestMapping("/setSession")
    public String setSession(HttpServletRequest request){
        //request 里面含有 session
        HttpSession session = request.getSession();
        session.setAttribute("好吃的","锅包肉");
        session.setAttribute("好炫的","溜肉段");
        return "设置Session成功";
    }
    @RequestMapping("/getSession")
    public String getSession(HttpServletRequest request){
        //request 里面含有 session
        HttpSession session = request.getSession();
        String s1 = (String) session.getAttribute("好吃的");
        String s2 = (String) session.getAttribute("好炫的");
        return "返回 session1 中的信息" + "," + s1 + "," + s2;
    }
    @RequestMapping("/getSession2")
    public String getSession2(HttpSession session){
        String s1 = (String) session.getAttribute("好吃的");
        String s2 = (String) session.getAttribute("好炫的");
        return "返回 session2 中的信息" + "," + s1 + "," + s2;
    }
    @RequestMapping("/getSession3")
    //只能获取一个session的key
    public String getSession3(@SessionAttribute("name") String name){
        return "返回 session3 中的信息" + name;
    }
    @RequestMapping("/getHeader")
    public String gerHeader(HttpServletRequest request){
        String userAgent = request.getHeader("User-Agent");
        return "User-Agent: " + userAgent;
    }
    @RequestMapping("/getHeader2")
    public String getHeader2(@RequestHeader("User-Agent") String userAgent){
        return "userAgent: " + userAgent;
    }
}
